package net.hn.hnms.biz.plan.controller;


import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.hn.hnms.biz.plan.request.DimDrivageFaceReq;
import net.hn.hnms.biz.plan.request.DimMiningFaceReq;
import net.hn.hnms.biz.plan.service.IDimDrivageFaceService;
import net.hn.hnms.biz.plan.service.IDimMiningFaceService;
import net.hn.hnms.biz.plan.service.IStatisticService;
import net.hn.hnms.biz.plan.vo.*;
import net.hn.hnms.biz.plan.vo.energy.OtherMonthCostVo;
import net.hn.hnms.biz.plan.vo.energy.YearEnergyStatisticsVo;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.utils.DateUtils;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Api(value = "统计相关", tags = {"统计相关"})
@RestController
@RequestMapping("/statistic")
@EnableScheduling
public class StatisticController {


    @Autowired
    private IStatisticService statisticService;
    @Autowired
    private IDimDrivageFaceService iDimDrivageFaceService;
    @Autowired
    private IDimMiningFaceService iDimMiningFaceService;

    /**
     * 生产统计
     */
    @ApiOperation("生产统计")
//    @SaCheckPermission("mining:face:add")
    @GetMapping("/bigscreen/produce")
    @AutoSetMineCode
    public R<ProduceStasticVO> produceForBigScreen(String date, String mineCode) {
        return R.ok(statisticService.produceForBigScreen(date, mineCode));
    }

    @ApiOperation("高头窑生产统计")
//    @SaCheckPermission("mining:face:add")
    @GetMapping("/bigscreen/gty/produce")
    @AutoSetMineCode
    public R<ProduceStasticVO> gtyProduceForBigScreen(String date, String mineCode) {
        return R.ok(statisticService.gtyProduceForBigScreen(date, mineCode));
    }

    /**
     * 月产量情况
     */
    @ApiOperation("月产量情况")
//    @SaCheckPermission("mining:face:add")
    @GetMapping("/month/yieldByMonth")
    @AutoSetMineCode
    public R<MonthYieldVo> yieldByMonth(String mineCode) {
        return R.ok(statisticService.yieldByMonth(mineCode));
    }

    /**
     * 年产量情况
     */
    @ApiOperation("年产量情况")
//    @SaCheckPermission("mining:face:add")
    @GetMapping("/year/yieldByYear")
    @AutoSetMineCode
    public R<YearYieldVo> yieldByYear(String mineCode) {
        return R.ok(statisticService.yieldByYear(mineCode));
    }

    /**
     * 月度计划/完成情况
     */
    @ApiOperation("月度计划/完成情况")
//    @SaCheckPermission("mining:face:add")
    @GetMapping("/month/completionRate")
    @AutoSetMineCode
    public R<CompletionRateVo> completionRate(String mineCode, String planDrivageName) {
        return R.ok(statisticService.completionRate(mineCode, planDrivageName));
    }

    @ApiOperation("昨日掘进进尺米数")
    @GetMapping("/drivage/getFootage")
    @AutoSetMineCode
    public R<Map<String, BigDecimal>> getDrivagefootage(String mineCode, @ApiParam("工作面名称") String planDrivageName) {
        return R.ok(statisticService.getDrivagefootage(mineCode, planDrivageName));
    }

    /**
     * 重点区域
     */
    @ApiOperation("重点区域")
//    @SaCheckPermission("mining:face:add")
    @GetMapping("/important/region")
    @AutoSetMineCode
    public R<List<ImportantRegionVo>> region(String mineCode) {
        List<ImportantRegionVo> list = new ArrayList<ImportantRegionVo>();
        if ("150621011038".equals(mineCode)) {
            DimDrivageFaceReq dimDrivageFaceReq = new DimDrivageFaceReq();
            dimDrivageFaceReq.setMineCode("150621011038");
            dimDrivageFaceReq.setIsKeyArea("1");
            DimMiningFaceReq dimMiningFaceReq = new DimMiningFaceReq();
            dimMiningFaceReq.setMineCode("150621011038");
            dimMiningFaceReq.setIsKeyArea("1");
            List<DimDrivageFaceVO> dimDrivageFaceVOS = iDimDrivageFaceService.selectList(dimDrivageFaceReq);
            List<DimMiningFaceVO> dimMiningFaceVOS = iDimMiningFaceService.selectList(dimMiningFaceReq);
            if (dimMiningFaceVOS != null) {
                for (DimMiningFaceVO dimDrivageFaceVO : dimMiningFaceVOS) {
                    ImportantRegionVo vo3 = new ImportantRegionVo();
                    vo3.setLable(dimDrivageFaceVO.getMiningFaceName());
                    vo3.setValue(0);
                    list.add(vo3);
                }
            }

            if (dimDrivageFaceVOS != null) {
                for (DimDrivageFaceVO dimDrivageFaceVO : dimDrivageFaceVOS) {
                    ImportantRegionVo vo3 = new ImportantRegionVo();
                    vo3.setLable(dimDrivageFaceVO.getDrivageFaceName());
                    vo3.setValue(0);
                    list.add(vo3);
                }
            }
//            ImportantRegionVo vo3 = new ImportantRegionVo();
//            vo3.setLable("204工作面");
//            vo3.setValue(204);
//            list.add(vo3);
//            ImportantRegionVo vo4 = new ImportantRegionVo();
//            vo4.setLable("301工作面");
//            vo4.setValue(301);
//            list.add(vo4);
//            ImportantRegionVo vo2 = new ImportantRegionVo();
//            vo2.setLable("108工作面");
//            vo2.setValue(108);
//            list.add(vo2);
//            ImportantRegionVo vo1 = new ImportantRegionVo();
//            vo1.setLable("202工作面");
//            vo1.setValue(202);
//            list.add(vo1);
        } else if ("620824009734".equals(mineCode)) {

            ImportantRegionVo vo1 = new ImportantRegionVo();
            vo1.setLable("250203下回采工作面");
            vo1.setValue(250203);
            list.add(vo1);
            ImportantRegionVo vo2 = new ImportantRegionVo();
            vo2.setLable("250209掘进工作面");
            vo2.setValue(250209);
            list.add(vo2);
            ImportantRegionVo vo3 = new ImportantRegionVo();
            vo3.setLable("150404掘进工作面");
            vo3.setValue(150404);
            list.add(vo3);
        } else if ("150781013671".equals(mineCode)) {
            DimDrivageFaceReq dimDrivageFaceReq = new DimDrivageFaceReq();
            dimDrivageFaceReq.setMineCode("150781013671");
            dimDrivageFaceReq.setIsKeyArea("0");
            DimMiningFaceReq dimMiningFaceReq = new DimMiningFaceReq();
            dimMiningFaceReq.setMineCode("150781013671");
            dimMiningFaceReq.setIsKeyArea("0");
            List<DimDrivageFaceVO> dimDrivageFaceVOS = iDimDrivageFaceService.selectList(dimDrivageFaceReq);
            List<DimMiningFaceVO> dimMiningFaceVOS = iDimMiningFaceService.selectList(dimMiningFaceReq);
            if (dimMiningFaceVOS != null) {
                for (DimMiningFaceVO dimDrivageFaceVO : dimMiningFaceVOS) {
                    ImportantRegionVo vo3 = new ImportantRegionVo();
                    vo3.setLable(dimDrivageFaceVO.getMiningFaceName());
                    vo3.setValue(0);
                    list.add(vo3);
                }
            }
            if (dimDrivageFaceVOS != null) {
                for (DimDrivageFaceVO dimDrivageFaceVO : dimDrivageFaceVOS) {
                    ImportantRegionVo vo3 = new ImportantRegionVo();
                    vo3.setLable(dimDrivageFaceVO.getDrivageFaceName());
                    vo3.setValue(0);
                    list.add(vo3);
                }
            }

        } else {
            DimDrivageFaceReq dimDrivageFaceReq = new DimDrivageFaceReq();
            dimDrivageFaceReq.setStatus("0");
            dimDrivageFaceReq.setMineCode(mineCode);
            DimMiningFaceReq dimMiningFaceReq = new DimMiningFaceReq();
            dimMiningFaceReq.setStatus("0");
            dimMiningFaceReq.setMineCode(mineCode);
            List<DimDrivageFaceVO> dimDrivageFaceVOS = iDimDrivageFaceService.selectList(dimDrivageFaceReq);
            List<DimMiningFaceVO> dimMiningFaceVOS = iDimMiningFaceService.selectList(dimMiningFaceReq);
            if (dimMiningFaceVOS != null) {
                for (DimMiningFaceVO dimDrivageFaceVO : dimMiningFaceVOS) {
                    ImportantRegionVo vo3 = new ImportantRegionVo();
                    vo3.setLable(dimDrivageFaceVO.getMiningFaceName());
                    vo3.setValue(0);
                    list.add(vo3);
                }
            }

            if (dimDrivageFaceVOS != null) {
                for (DimDrivageFaceVO dimDrivageFaceVO : dimDrivageFaceVOS) {
                    ImportantRegionVo vo3 = new ImportantRegionVo();
                    vo3.setLable(dimDrivageFaceVO.getDrivageFaceName());
                    vo3.setValue(0);
                    list.add(vo3);
                }
            }
        }
        return R.ok(list);
    }

    /**
     * 年度计划/完成情况
     */
    @ApiOperation("年度计划/完成情况")
//    @SaCheckPermission("mining:face:add")
    @GetMapping("/year/completionRate")
    @AutoSetMineCode
    public R<CompletionRateVo> completionRateByYear(String mineCode, String planDrivageName) {
        return R.ok(statisticService.completionRateByYear(mineCode, planDrivageName));
    }

    /**
     * 月产量曲线
     */
    @ApiOperation("月产量曲线")
//    @SaCheckPermission("mining:face:add")
    @GetMapping("/month/outputCurve")
    @AutoSetMineCode
    public R<List<PlanCurveVo>> outputCurve(String mineCode) {
        return R.ok(statisticService.outputCurve(mineCode));
    }

    /**
     * 月计划产量曲线
     */
    @ApiOperation("月计划产量曲线已废弃")
//    @SaCheckPermission("mining:face:add")
    @GetMapping("/month/planOutputCurve")
    @AutoSetMineCode
    public R<List<Map<String, Object>>> planOutputCurve(String mineCode) {
        return R.ok(statisticService.planOutputCurve(mineCode));
    }


    @ApiOperation("经营综合概览-生成计划统计")
//    @SaCheckPermission("mining:face:add")
    @GetMapping("/plan/complete")
    @AutoSetMineCode
    public R<Map<String, PlanStatisticVO>> planComplete(Integer month, String mineCode) {
        return R.ok(statisticService.planComplete(month, mineCode));
    }


    @ApiOperation("经营综合概览-其他生产计划")
//    @SaCheckPermission("mining:face:add")
    @GetMapping("/plan/other")
    @AutoSetMineCode
    public R<Map<String, PlanStatisticVO>> planOther(Integer month, String mineCode) {
        return R.ok(statisticService.planOther(month, mineCode));
    }


    @ApiOperation("经营综合概览-经营状况")
//    @SaCheckPermission("mining:face:add")
    @GetMapping("/other/actual")
    @AutoSetMineCode
    public R<ExecutionInfoVO> otherActual(Integer month, String mineCode) {
        return R.ok(statisticService.otherActual(month, mineCode));
    }


    @ApiOperation("经营综合概览-经营状况")
//    @SaCheckPermission("mining:face:add")
    @GetMapping("/wegg")
    @AutoSetMineCode
    public R<WeggVO> wegg(Integer month, String mineCode) {
        return R.ok(statisticService.wegg(month, mineCode));
    }


    @ApiOperation("经营综合概览-销售计划")
//    @SaCheckPermission("mining:face:add")
    @GetMapping("/salePlan")
    @AutoSetMineCode
    public R<List<MonthValue>> salePlan(Integer month, String mineCode) {
        return R.ok(statisticService.salePlan(month, mineCode));
    }

    @ApiOperation("高头窑经营综合概览-销售计划")
    @GetMapping("/gtySalePlan")
    @AutoSetMineCode
    public R<List<MonthValue>> gtySalePlan(Integer month, String mineCode) {
        return R.ok(statisticService.gtySalePlan(month, mineCode));
    }


    @ApiOperation("生产成本分析-其他生产成本")
//    @SaCheckPermission("mining:face:add")
    @GetMapping("/product/otherCostByYear")
    @AutoSetMineCode
    public R<List<MonthValue>> otherCostByYear(Integer year, String mineCode) {
        return R.ok(statisticService.otherCost(year, null, mineCode));
    }


    @ApiOperation("生产成本分析-其他生产成本")
//    @SaCheckPermission("mining:face:add")
    @GetMapping("/product/otherCostByMonth")
    @AutoSetMineCode
    public R<List<MonthValue>> otherCostByMonth(@DateTimeFormat(pattern = "yyyy-MM") Date date, String mineCode) {
        Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
        calendar.add(Calendar.MONTH, -1);
        date = date == null ? calendar.getTime() : date;
        return R.ok(statisticService.otherCost(DateUtil.year(date), DateUtil.month(date) + 1, mineCode));
    }


    @ApiOperation("生产综合能效分析-采掘比")
//    @SaCheckPermission("mining:face:add")
    @GetMapping("/efficiency/drivagePercent")
    @AutoSetMineCode
    public R<List<MonthValue>> drivagePercent(Integer year, String mineCode) {
        return R.ok(statisticService.drivagePercent(year, mineCode));
    }


    @ApiOperation("生产综合能效分析-销售计划与执行")
//    @SaCheckPermission("mining:face:add")
    @GetMapping("/efficiency/salePlanAndExecution")
    @AutoSetMineCode
    public R<List<MonthValue>> salePlanAndExecution(Integer year, String mineCode) {
        return R.ok(statisticService.salePlanAndExecution(year, mineCode));
    }


    @ApiOperation("生产综合能效分析-能效趋势")
//    @SaCheckPermission("mining:face:add")
    @GetMapping("/efficiency/trend")
    @AutoSetMineCode
    public R<List<MonthValue>> efficiencyTrend(Integer year, String mineCode) {
        return R.ok(statisticService.efficiencyTrend(year, mineCode));
    }


    @ApiOperation("生产综合能效分析-产量计划与执行")
//    @SaCheckPermission("mining:face:add")
    @GetMapping("/efficiency/planOutputAndRealOutput")
    @AutoSetMineCode
    public R<List<MonthValue>> planOutputAndRealOutput(Integer year, String mineCode) {
        return R.ok(statisticService.planOutputAndRealOutput(year, mineCode));
    }

    @ApiOperation("生产综合能效分析-全员工效")
//    @SaCheckPermission("mining:face:add")
    @GetMapping("/efficiency/staffWork")
    @AutoSetMineCode
    public R<List<MonthValue>> staffWork(Integer year, String mineCode) {
        return R.ok(statisticService.staffWork(year, mineCode));
    }

    @ApiOperation("生产综合能效分析-吨煤生产成本")
//    @SaCheckPermission("mining:face:add")
    @GetMapping("/efficiency/coalProductCost")
    @AutoSetMineCode
    public R<List<MonthValue>> coalProductCost(Integer year, String mineCode) {
        return R.ok(statisticService.coalProductCost(year, mineCode));
    }

    @ApiOperation("生产综合能效分析-产量与销量")
//    @SaCheckPermission("mining:face:add")
    @GetMapping("/efficiency/realOutputAndSale")
    @AutoSetMineCode
    public R<List<MonthValue>> realOutputAndSale(Integer year, String mineCode) {
        return R.ok(statisticService.realOutputAndSale(year, mineCode));
    }


    @ApiOperation("生产综合能效分析-回采/掘进趋势")
//    @SaCheckPermission("mining:face:add")
    @GetMapping("/efficiency/miningOrDrivageTrend")
    @AutoSetMineCode
    public R<List<MonthValue>> miningOrDrivageTrend(Integer type, Integer year, String mineCode) {
        return R.ok(statisticService.miningOrDrivageTrend(type, year, mineCode));
    }

    @ApiOperation("月产量统计分析--高头窑")
    @GetMapping("/month/yieldStatisticalAnalysis")
    @AutoSetMineCode
    public R<List<MonthYieldVo>> yieldStatisticalAnalysis(Integer year, Integer month, String mineCode) {
        if (year == null && month == null) {
            Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
            calendar.add(Calendar.MONTH, 1);
            Date date = null;
            date = date == null ? calendar.getTime() : date;
            String lastMonths[] = DateUtils.getLastMonth(new SimpleDateFormat("yyyy-MM-dd").format(date), 2);
            year = Integer.valueOf(lastMonths[1]);
            month = Integer.valueOf(lastMonths[2]);
        }
        return R.ok(statisticService.yieldStatisticalAnalysis(year, month, mineCode));
    }

    @ApiOperation("定时计算库存数据并插入，每周五10点运行")
    @Scheduled(cron = "0 50 16 ? * WED")
    @GetMapping("/timingYieldStatisticalAnalysis")
    public R<Void> add() {
        System.out.println("-----------------------------------执行定时--------------------------");
        statisticService.timingYieldStatisticalAnalysis();
        return R.ok();
    }


    @ApiOperation("原煤库存统计库存--高头窑")
    @GetMapping("/year/stock")
    @AutoSetMineCode
    public R<StatisticalYearVo> stock(Integer year, String mineCode) {
        if (year == null) {
            Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
            calendar.add(Calendar.MONTH, 1);
            Date date = null;
            date = date == null ? calendar.getTime() : date;
            String lastMonths[] = DateUtils.getLastMonth(new SimpleDateFormat("yyyy-MM-dd").format(date), 2);
            year = Integer.valueOf(lastMonths[1]);
        }
        return R.ok(statisticService.stock(year, mineCode));
    }

    @ApiOperation("原煤库存统计自用煤--高头窑")
    @GetMapping("/year/backCoal")
    @AutoSetMineCode
    public R<StatisticalYearVo> backCoal(Integer year, String mineCode) {
        if (year == null) {
            Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
            calendar.add(Calendar.MONTH, 1);
            Date date = null;
            date = date == null ? calendar.getTime() : date;
            String lastMonths[] = DateUtils.getLastMonth(new SimpleDateFormat("yyyy-MM-dd").format(date), 2);
            year = Integer.valueOf(lastMonths[1]);
        }
        return R.ok(statisticService.backCoal(year, mineCode));
    }

    @ApiOperation("原煤库存统计煤泥--高头窑")
    @GetMapping("/year/coalSlime")
    @AutoSetMineCode
    public R<StatisticalYearVo> coalSlime(Integer year, String mineCode) {
        if (year == null) {
            Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
            calendar.add(Calendar.MONTH, 1);
            Date date = null;
            date = date == null ? calendar.getTime() : date;
            String lastMonths[] = DateUtils.getLastMonth(new SimpleDateFormat("yyyy-MM-dd").format(date), 2);
            year = Integer.valueOf(lastMonths[1]);
        }
        return R.ok(statisticService.coalSlime(year, mineCode));
    }

    @ApiOperation("原煤库存统计矸石--高头窑")
    @GetMapping("/year/shaleAmount")
    @AutoSetMineCode
    public R<StatisticalYearVo> shaleAmount(Integer year, String mineCode) {
        if (year == null) {
            Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
            calendar.add(Calendar.MONTH, 1);
            Date date = null;
            date = date == null ? calendar.getTime() : date;
            String lastMonths[] = DateUtils.getLastMonth(new SimpleDateFormat("yyyy-MM-dd").format(date), 2);
            year = Integer.valueOf(lastMonths[1]);
        }
        return R.ok(statisticService.shaleAmount(year, mineCode));
    }

    @ApiOperation("原煤库存统计销量--高头窑")
    @GetMapping("/year/sale")
    @AutoSetMineCode
    public R<StatisticalYearVo> sale(Integer year, String mineCode) {
        if (year == null) {
            Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
            calendar.add(Calendar.MONTH, 1);
            Date date = null;
            date = date == null ? calendar.getTime() : date;
            String lastMonths[] = DateUtils.getLastMonth(new SimpleDateFormat("yyyy-MM-dd").format(date), 2);
            year = Integer.valueOf(lastMonths[1]);
        }
        return R.ok(statisticService.sale(year, mineCode));
    }

    @ApiOperation("销售统计分析--高头窑")
    @GetMapping("/saleStatistic")
    @AutoSetMineCode
    public R<StatisticalWeekVo> statisticSale(@DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate, @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate, String mineCode) {
//        if(year==null){
//            Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
//            calendar.add(Calendar.MONTH, 1);
//            Date date = null;
//            date = date == null ? calendar.getTime() : date;
//            String lastMonths[] = DateUtils.getLastMonth(new SimpleDateFormat("yyyy-MM-dd").format(date), 2);
//            year = Integer.valueOf(lastMonths[1]);
//        }
        return R.ok(statisticService.statisticSale(beginDate, endDate, mineCode));
    }

    @ApiOperation("生产综合概览-近6个月计划达成情况,取产量(回采+掘进)数据")
//    @SaCheckPermission("mining:face:add")
    @GetMapping("/achievementsLastSixMonths")
    @AutoSetMineCode
    public R<List<MonthValue>> achievementsLastSixMonths(String companyCode) {
        return R.ok(statisticService.achievementsLastSixMonths(companyCode));
    }

    @ApiOperation("经营综合概览-近6月销售计划达成情况")
    @GetMapping("/gtySalePlanLastSixMonths")
    @AutoSetMineCode
    public R<List<MonthValue>> gtySalePlanLastSixMonths(String companyCode) {
        return R.ok(statisticService.gtySalePlanLastSixMonths(companyCode));
    }

    @ApiOperation("水电能耗分析看板-产量、吨煤电耗、吨煤水耗")
    @GetMapping("/yearOutputCurve")
    @AutoSetMineCode
    public R<List<YearEnergyStatisticsVo>> yearOutputCurve(String mineCode, Integer year) {
        return R.ok(statisticService.getYearOutputCurve(mineCode, year));
    }

    @ApiOperation("水电能耗分析看板-电费分析(total2)、用电量分析(total3)、水费分析(total)、用水分析(total1)  产量(total4)")
    @GetMapping("/yearOutputEnergyCurve")
    @AutoSetMineCode
    public R<List<MonthValue>> yearOutputEnergyCurve(String mineCode, Integer year) {
        return R.ok(statisticService.getYearOutputEnergyCurve(mineCode, year));
    }

    @ApiOperation("用电量与产量同环比  产量同比(total) 产量环比(total1) 用电量同比(total2) 用电量环比(total3) 用水量同比(total4) 用水量环比(total5)")
    @GetMapping("/electricityWaterAndOutput")
    public R<List<MonthValue>> electricityAndOutput(Integer year, String mineCode) {
        return R.ok(statisticService.getElectricityAndOutput(year, mineCode));
    }


    @ApiOperation("生产成本分析--本月其他成本消耗")
    @GetMapping("/otherMonthCost")
    @AutoSetMineCode
    public R<List<OtherMonthCostVo>> otherMonthCost(String searchDate, String mineCode) {
        return R.ok(statisticService.otherMonthCost(searchDate, mineCode));
    }

    @ApiOperation("生产成本分析--生产用水 南风井(total)  副井(total1)")
    @GetMapping("/productionWaterSupply")
    @AutoSetMineCode
    public R<List<MonthValue>> productionWaterSupply(Integer year, String mineCode) {
        return R.ok(statisticService.getProductionWaterSupply(year, mineCode));
    }


}
